package app.models.activity;

import com.github.sog.annotation.TableBind;
import com.github.sog.config.StringPool;
import com.github.sog.controller.datatables.core.ColumnDef;
import com.github.sog.controller.datatables.core.DatatablesCriterias;
import com.github.sog.plugin.sqlinxml.SqlKit;
import com.google.common.base.Strings;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

/**
 * <p>
 * .商场活动报名
 * </p>
 *
 * @author liuhui
 * @version 1.0 2014-04-25 16:46
 * @since JDK 1.6
 */
@TableBind(tableName = "ol_mall_enroll")
public class MallEnroll extends Model<MallEnroll> {

    public static final MallEnroll dao = new MallEnroll();
    private static final long serialVersionUID = -5130374026787778699L;

    public Page<MallEnroll> datatable(DatatablesCriterias criterias) {
        int pageSize = criterias.getDisplaySize();
        int start = criterias.getDisplayStart() / pageSize + 1;
        final List<ColumnDef> sortingColumnDefs = criterias.getSortingColumnDefs();

        String where = SqlKit.sql("mallenroll.columns-where");
        if (sortingColumnDefs != null && !sortingColumnDefs.isEmpty()) {
            StringBuilder orderBy = new StringBuilder();
            for (ColumnDef sortingColumnDef : sortingColumnDefs)
                if (sortingColumnDef.isSortable()) {
                    orderBy.append(sortingColumnDef.getName()).append(StringPool.SPACE).append(sortingColumnDef.getSortDirection().toString());
                }
            final String byColumns = orderBy.toString();
            if(!Strings.isNullOrEmpty(byColumns)){
                where += " ORDER BY " + byColumns;
            }
        }
        if (!where.contains("ORDER")) {
            where += " ORDER BY id DESC";
        }

        return paginate(start, pageSize, SqlKit.sql("mallenroll.columns-select"), where);
    }

    /**
     * 根据姓名/电话/活动id查询
     * @param name
     * @param phone
     * @param aid
     * @return
     */
    public MallEnroll getEnrollByNPA(String name, String phone, String aid) {
        return findFirst(SqlKit.sql("mallenroll.getEnrollByNPA"), name, phone, aid);
    }

    public MallEnroll getEnrollByMid(long mid,long aid){
        return findFirst(SqlKit.sql("mallenroll.getEnrollByMid"),mid,aid);
    }
}
